﻿using Dwgc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace Dwg_output.DCSTag
{
    class Output_DcsTag
    {
        private List<Model_Base_Block>  model_Base_Blocks = new List<Model_Base_Block>() { };
        private System.Data.DataTable dataTable;
        private System.Data.DataTable dataTable_Block;
        string dbpath = string.Empty;
        List<string> lossblockList = new List<string>() { };
        public Output_DcsTag(string path)
        {
            dbpath = path;
          



        }

        public List<string> init_block(Database SourceDb, Database destDb)
        {

            dataTable_Block = Read_DatableSql.ReadBaseBlockFromDB();
            dataTable = Read_DatableSql.ReadDeviceTable(dbpath);
            var distanceName = dataTable.AsEnumerable()
                                .Select(row => row.Field<string>("model"))
                                .Distinct()
                                .ToList();

            foreach (var item in distanceName)
            {

                DataRow dataRow = dataTable_Block.AsEnumerable()
                                     .Where(row => row.Field<string>("model") == item.ToString())
                                     .FirstOrDefault();

                if (dataRow!=null)
                {
                    ObjectId CopyBlockRecords;
                    if (string.IsNullOrEmpty(dataRow.Field<string>("att1")) && string.IsNullOrEmpty(dataRow.Field<string>("att2")))
                    {

                        CopyBlockRecords = CopyBlock.CopyBlocks(SourceDb, destDb, dataRow.Field<string>("name"), dataRow.Field<string>("name"));
                    }
                    else
                    {
                        CopyBlockRecords = CopyAttributeBlock.CopyAttributeBlocks(SourceDb, destDb, dataRow.Field<string>("name"), dataRow.Field<string>("name"));
                    }
                    Model_Base_Block block = new Model_Base_Block
                    {
                        ID = dataRow.Field<long?>("id") != null ? (int)dataRow.Field<long>("id") : 0,
                        Dir = dataRow.Field<string>("dir") ?? "",
                        Model = dataRow.Field<string>("model") ?? "",
                        Name = dataRow.Field<string>("name") ?? "",
                        Xlen = dataRow.Field<double?>("xlen") ?? 0.0,
                        Ylen = dataRow.Field<double?>("ylen") ?? 0.0,
                        Att1 = dataRow.Field<string>("att1") ?? "",
                        Att2 = dataRow.Field<string>("att2") ?? "",
                        CopyBlockRecord = CopyBlockRecords
                    };

                    model_Base_Blocks.Add(block);



                }
                else
                {
                    lossblockList.Add(item);
                }
            }

            return lossblockList;
        }

        public void DrawDevice(Database destDb)
        {
            CAD_Tool cAD_Tool = new CAD_Tool();
            cAD_Tool.CopyNullAttrBlock_Tool(destDb, new Point3d(0, 0, 0), model_Base_Blocks, "CM901");
            DrawDevice_Tag(dataTable, destDb);

        }
        private void DrawDevice_Tag(System.Data.DataTable dataTable, Database destDb)
        {
            int canbinet_id = 0;
            
            int x = 0;//图纸增幅
            int y = 0;//图纸增幅
            Point3d point_Source = new Point3d(0, 0, 0);


            List<long> cabinet_ids = dataTable.AsEnumerable()
              .Select(row => row.Field<long>("cabinet_id"))
              .Distinct()
              .ToList();


            foreach (var item in cabinet_ids)
            {
                var curcabinet =  dataTable.AsEnumerable()
                    .Where(row => row.Field<long>("cabinet_id") == item )
                    ;
                    
                foreach (DataRow row in curcabinet)
                {
                    canbinet_id = (Int32)row.Field<long>("cabinet_id");
                    string model = row.Field<string>("model");
                    string name = row.Field<string>("name");
                    string kind = row.Field<string>("kind");
                    int control_id = (Int32)row.Field<long>("control_id");
                    int node_addr = (Int32)row.Field<long>("node_addr");
                    int rack_addr = (Int32)row.Field<long>("rack_addr");
                    int card_addr = (Int32)row.Field<long>("card_addr");
                    CAD_Tool cAD_Tool = new CAD_Tool();

                    Point3d point3Drack = new Point3d(340*rack_addr, -1800 * y,0);
                    Point3d point3DNodeSource = point3Drack.Add(new Vector3d(-10.78,-53.12,0)); ;
                    Point3d point3DNode = point3DNodeSource.Add( new Vector3d(0, -134.38 * node_addr, 0));
                    Point3d point3DTag = point3DNode.Add(new Vector3d(19, -5.71, 0));


                    if (kind.Equals("机架"))
                    {

                      //  point3Drack = point3Drack.Add(new Vector3d(0, 0, 0));
                        cAD_Tool.CopyAttrBlock2Att_Tool(destDb, model_Base_Blocks, point3Drack, model, name, new string[0]);

                    }
                   else if (kind.Equals("基座"))
                    {
                       // point3DNode = point3DNodeSource.Add(new Vector3d(0,-134.38* node_addr,0));
                        cAD_Tool.CopyAttrBlock2Att_Tool(destDb, model_Base_Blocks, point3DNode, model, name, new string[0]);

                    }
                    else if (kind.Equals("卡件"))
                    {
                        int rorl = card_addr % 2 == 1? 0:1;

                        point3DTag = point3DTag.Add(new Vector3d(45.2 * rorl, 0, 0));
                        cAD_Tool.CopyAttrBlock2Att_Tool(destDb, model_Base_Blocks, point3DTag, model, name, new string[0]);

                    }






                }


                y++;
            }
           
        
        
        
        }
    }
    class CAD_Tool
    {
        public void CopyAttrBlock2Att_Tool(Database destDb, List<Model_Base_Block> model_Base_Blocks, Point3d point3D, string block_name, string name, string[] cap)
        {
            var model = model_Base_Blocks.FirstOrDefault(t => t.Model == block_name);
            if (model==null)
            {
                return;
            }
            BlockReference blkRef2 = CopyAttributeBlock.AddAttributeReference(destDb, model.CopyBlockRecord, point3D, 0, 1);
            CopyAttributeBlock.GetAttributeVal(destDb, blkRef2, model.Name);
            CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, model.Att1, name);
            if (!string.IsNullOrEmpty(model.Att2) && cap.Length >= 2)
            {
                CopyAttributeBlock.SetAttributeVal(destDb, blkRef2, model.Att2, cap[1]);
            }

            blkRef2.Dispose();
        }
        public void CopyNullAttrBlock_Tool(Database destDb, Point3d point3D, List<Model_Base_Block> model_Base_Blocks, string block_name)
        {
            var model = model_Base_Blocks.FirstOrDefault(t => t.Model == block_name);
            if (model == null)
            {
                return;
            }
            CopyBlock.AddReference(destDb, model.CopyBlockRecord, point3D, 0, 1);

        }
    }
    //string sql_jigui = "SELECT id,model,name,kind,ter_pair FROM device WHERE kind IN (" + kinds + ") AND cabinet_id = @cabinet_id";
    //SQLiteParameter[] param = new SQLiteParameter[]{
    //                                                        new SQLiteParameter("@cabinet_id", cabinet_id)
    //                                                    };
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    //SQLiteHelper sqlhelper = new SQLiteHelper(DBpath, ""); // 参数1: db的绝对路径 参数2: 数据库密码（如果为空则不设置）
    //SQLiteParameter[] param1 = new SQLiteParameter[]
    //{
    //                       new SQLiteParameter("@cabinet_id", cabinet_id)
    //                                            };

    //List<Student> students = new List<Student>() { };
    //DataTable reader = sqlhelper.ExecuteReader_V3(sql_jigui, param1);
}
